package com.fujica.db_operation.server.impl;

import com.fujica.db_operation.entity.TreeNode;
import com.fujica.db_operation.server.TreeOrderService;
import org.springframework.stereotype.Service;

/**
 * @ClassName TreeOrderServiceImpl
 * @Description TODO
 * @Author admin
 * @Date 2020/7/6 9:19
 * @Version 1.0
 **/

@Service
public class TreeOrderServiceImpl implements TreeOrderService {

    // 前序遍历
    @Override
    public void preOrderMethod(TreeNode treeNode) {

        if (treeNode != null) {
            System.out.print(treeNode.getData() + " ");
            if (treeNode.getLeft() != null) {
                // 递归调用
                preOrderMethod(treeNode.getLeft());
            }
            if (treeNode != null) {
                // 递归调用
                preOrderMethod(treeNode.getRight());
            }
        }

    }

    // 中序遍历
    @Override
    public void medOrderMethod(TreeNode treeNode) {

        if (treeNode != null) {
            if (treeNode.getLeft() != null) {
                medOrderMethod(treeNode.getLeft());
            }
            System.out.print(treeNode.getData() + " ");
            if (treeNode.getRight() != null) {
                medOrderMethod(treeNode.getRight());
            }
        }

    }

    // 后序遍历
    @Override
    public void postOrderMethod(TreeNode treeNode) {

        if (treeNode != null) {
            if (treeNode.getRight() != null) {
                postOrderMethod(treeNode.getRight());
            }
            if (treeNode.getLeft() != null) {
                postOrderMethod(treeNode.getLeft());
            }
            System.out.print(treeNode.getData() + " ");
        }

    }
}
